package cn.crm.spring.service;

import cn.crm.spring.pojo.Dict;
import cn.crm.spring.repository.DictRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service("dictService")
public class DictServiceImpl implements DictService{
    @Resource
    private DictRepository dictRepository;

    @Override
    public Page<Dict> findDict(Integer dictId, String dictItem, Pageable pageable,Integer typeId) {
        Specification<Dict> specification=new Specification<Dict>() {
            @Override
            public Predicate toPredicate(Root<Dict> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if (dictId!=null&&dictId!=0){
                    predicates.add(criteriaBuilder.equal(root.get("dictId"),dictId));
                }
                if (dictItem != null && !dictItem.equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("dictItem"), "%" + dictItem + "%"));
                }
                if (typeId==1){
                    predicates.add(criteriaBuilder.equal(root.get("dictType"),"客户等级"));
                }
                if (typeId==2){
                    predicates.add(criteriaBuilder.equal(root.get("dictType"),"服务类型"));
                }
                if (typeId==3){
                    predicates.add(criteriaBuilder.equal(root.get("dictType"),"地区"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return dictRepository.findAll(specification,pageable);
    }

    @Override
    public void saveDict(Dict dict) {
        dictRepository.save(dict);
    }

    @Override
    public void deleteDict(Integer id) {
        dictRepository.deleteById(id);
    }

    @Override
    public Dict getDictByDictId(Integer dictId) {
        return dictRepository.findById(dictId).get();
    }

    @Override
    public List<Dict> findSvrType() {
        Specification<Dict> specification = new Specification<Dict>() {
            @Override
            public Predicate toPredicate(Root<Dict> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                predicates.add(criteriaBuilder.equal(root.get("dictType"), "服务类型"));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return dictRepository.findAll(specification);
    }
}
